package com.teziyou.commodity.web;

import java.util.Arrays;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teziyou.commodity.constant.Global;
import com.teziyou.commodity.logic.AreaLogic;
import com.teziyou.commodity.logic.SceneLogic;
import com.teziyou.commodity.service.DictService;
import com.teziyou.commodity.util.ConvertUtil;
import com.teziyou.commodity.util.Maps;
import com.teziyou.commodity.util.PageUtil;

@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/scene")
@Controller
public class SceneController extends BaseController {
	
	@Autowired
	private DictService dictService;
	@Autowired
	private AreaLogic areaLogic;
	@Autowired
	private SceneLogic sceneLogic;
	
	@RequestMapping("/add_view")
	public String add(Map map) {
		map.put("levels", dictService.getDictionaryList(Global.DictType.scene_level));
		map.put("provinces", areaLogic.getAdministrativeListByPid(1001, -1));
		return "scene/add";
	}
	
	@ResponseBody
	@RequestMapping("/add")
	public String doAdd(HttpServletRequest req) {
		Map<String, String> map = super.getParaMap(req);
		//System.err.println(super.getParaMap(req));
		System.err.println(map);
		boolean flag = sceneLogic.addScene(map);
		return flag?success("保存成功！！"):fail("保存失败！！");
	}
	
	@RequestMapping("/list")
	public String list(HttpServletRequest req) {
		req.setAttribute("levels", dictService.getDictionaryList(Global.DictType.scene_level));
		req.setAttribute("provinces", areaLogic.getAdministrativeListByPid(1001, -1));
		req.setAttribute("scenes", sceneLogic.findSceneData(Maps.newHashMap()));
		Map page = Maps.newHashMap("pageNo", PageUtil.initPageNo(req.getParameter("pageNo"))
				, "total", sceneLogic.findScenesCount(Maps.newHashMap()), "pageSize", Global.pageSize);
		req.setAttribute("page", page);
		return "scene/list";
	}
	
	@RequestMapping("/query")
	public String table(HttpServletRequest req) {
		Map param = super.getParaMap(req);
		System.err.println(param);
		req.setAttribute("scenes", sceneLogic.findSceneData(param));
		Map page = Maps.newHashMap("pageNo", PageUtil.initPageNo(req.getParameter("pageNo"))
				, "total", sceneLogic.findScenesCount(param), "pageSize", Global.pageSize);
		req.setAttribute("page", page);
		return "scene/table";
	}
	
	@RequestMapping("/dialog")
	public String dialog() {
		return "scene/dialog";
	}
	
	@ResponseBody
	@RequestMapping("/del_scene")
	public String deleteScene(HttpServletRequest req) {
		String[] sceneIds = req.getParameterValues("sceneId");
		System.out.println(Arrays.asList(sceneIds));
		return success("删除成功！！");
	}
	
	@RequestMapping("/edit_view")
	public String editView(long id, ModelMap map) {
		Map scene = sceneLogic.findSceneById(id);
		System.out.println(scene);
		map.addAttribute("scene", scene);
		map.addAttribute("levels", dictService.getDictionaryList(Global.DictType.scene_level));
		map.addAttribute("provinces", areaLogic.getAdministrativeListByPid(1001, -1));
		int districtId = ConvertUtil.toInt(scene.get("adm_div_id"));
		Map parents = areaLogic.getParentMapByDistrictId(districtId);
		System.err.println(parents);
		//map.addAttribute("citys", parents.get("citys"));
		//map.addAttribute("districts", parents.get("districts"));
		map.addAllAttributes(parents);
		return "/scene/add";
	}
	
}
